package com.sibbay.vta.adaptor;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sibbay.tools.Tools;
import com.sibbay.vta.R;

public final class TimeListAdapter extends BaseAdapter {
	private static final String TAG = "TimeListAdapter";

	private final Context mContext;
	private final int mSelectedColor;
//	private final int mTextEnabledColor;
//	private final int mTextDisabledColor;
	private final String[][] mTimeTable;
	private final boolean[] mTimeListSelected;
	private int mTimeListSelectedPos = 0;
	private int mStation = 0;
	private String mCurrenTime;
	private Drawable mDefBackground;

    public TimeListAdapter(final Context context, final String[][] timeTable, int maxSize) {
    	mContext = context;
    	mTimeTable = timeTable;
    	mTimeListSelected = new boolean[maxSize];
    	mCurrenTime = Tools.getCurrentTime();
    	mSelectedColor = mContext.getResources().getColor(R.color.time_selected);
//    	mTextEnabledColor = mContext.getResources().getColor(R.color.time_text_enabled);
//    	mTextDisabledColor = mContext.getResources().getColor(R.color.time_text_disabled);
    }

    @Override
    public int getCount() {
        return mTimeTable[mStation].length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewGroup tg;
        if (convertView == null) {
            tg = (ViewGroup) LayoutInflater.from(mContext).inflate(R.layout.time_list_item, parent, false);
            if (mDefBackground == null) {
            	mDefBackground = tg.getBackground();
            }
        } else {
            tg = (ViewGroup) convertView;
        }
        final String s = mTimeTable[mStation][position];
        ((TextView)tg.findViewById(R.id.text)).setText(s);
        if (mTimeListSelected[position] == true) {
        	tg.setBackgroundColor(mSelectedColor);
        } else {
        	tg.setBackgroundDrawable(mDefBackground);
//        	tg.setBackgroundColor(Color.TRANSPARENT);
        }
        	
        if (mTimeListSelectedPos >= 0 && position >= mTimeListSelectedPos) {
        	final int diff = Tools.convertToInt(mTimeTable[mStation][position]) - Tools.convertToInt(mCurrenTime); 
        	((TextView)tg.findViewById(R.id.text_diff)).setText(Tools.convertToStringDiff(diff));
        } else {
        	((TextView)tg.findViewById(R.id.text_diff)).setText("");
        }
        
        return tg;
    }
    
    public int setStation(int station) {
//    	Log.d(TAG, "setStation: " + station);
    	mStation = station;
    	FindBestTimePosition.setArray(mTimeTable[mStation]);
    	final int pos = findBestPosition();
    	setSelected(pos);
    	return pos;
    }
    
    public void setSelected(int pos) {
//    	Log.d(TAG, "setSelected: " + pos);
    	if (mTimeListSelectedPos >= 0)
    		mTimeListSelected[mTimeListSelectedPos] = false;
    	
    	mTimeListSelectedPos = pos;
    	if (pos >= 0)
    		mTimeListSelected[mTimeListSelectedPos] = true;
    	
    	notifyDataSetChanged();
    }

    private int findBestPosition() {
    	mCurrenTime = Tools.getCurrentTime();
    	return FindBestTimePosition.findPosition(mCurrenTime);
    }

    // find best time in sorted array of time and having AM time in the end of array
    // example: "08:29P", "11:00P", "11:30P", "12:01A",
    static final class FindBestTimePosition {
    	
    	private static String[] sTimeArray;
    	private static int sAmIndex = 0;
    	
    	static void setArray(final String[] arr) {
    		sTimeArray = arr;
    		sAmIndex = 0;
    		for (int i = arr.length-1; i >= 0; --i) {
    			// 12:01A
    			// 012345
    			if (arr[i].charAt(5) == 'A') 
    				sAmIndex = i;
    			else
    				break;
    		}
    	}

    	// @see Tools.binSearch
        static int findPosition(final String time) {
        	if (sAmIndex == 0) {
		    	return Tools.binSearch(sTimeArray, 0, sTimeArray.length, time);
        	}
        	
        	int result = Tools.binSearch(sTimeArray, 0, sAmIndex, time);
        	if (result >= 0)
        		return result;
        	
        	result = Tools.binSearch(sTimeArray, sAmIndex, sTimeArray.length, time);
        	if (result >= 0)
        		return result;
        	
        	return result;
        }
        
    }
    
    /// 
    
}
